package controllers;

import dao.*;
import models.DeliveryInfo_T_Entity;
import play.data.FormFactory;
import play.libs.Json;
import play.libs.concurrent.HttpExecutionContext;
import play.mvc.Controller;
import play.mvc.Result;

import javax.inject.Inject;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletionStage;

import com.fasterxml.jackson.databind.JsonNode;  
import com.fasterxml.jackson.databind.node.ObjectNode;


/**
 * The controller keeps all database operations behind the repository, and uses
 * {@link play.libs.concurrent.HttpExecutionContext} to provide access to the
 * {@link play.mvc.Http.Context} methods like {@code request()} and {@code flash()}.
 */
public class DeliveryInfoController extends Controller {

    private final FormFactory formFactory;
    private final DeliveryInfo_Repository deliveryInfo_Repository;
    private final HttpExecutionContext ec;

    @Inject
    public DeliveryInfoController(FormFactory formFactory, DeliveryInfo_Repository deliveryInfo_Repository, HttpExecutionContext ec) {
        this.formFactory = formFactory;
        this.deliveryInfo_Repository = deliveryInfo_Repository;
        this.ec = ec;
    }
    
    public CompletionStage<Result> GetDeliveryInfo() {
		JsonNode json = request().body().asJson();
		String DeliveryDate = json.findPath("DeliveryDate").textValue();
		String PhoneNumber = json.findPath("PhoneNumber").textValue();
		String KHMC = json.findPath("KHMC").textValue();
		String HWMC = json.findPath("HWMC").textValue();
		String DeliveryOrderId = json.findPath("DeliveryOrderId").textValue();
		String ParentSubId = json.findPath("ParentSubId").textValue();
		String SubId = json.findPath("SubId").textValue();
    	
	    return deliveryInfo_Repository.GetDeliveryInfo(DeliveryDate, PhoneNumber, KHMC, HWMC, DeliveryOrderId, ParentSubId, SubId).thenApplyAsync(_GetListByFunc -> {
    		return ok(Json.toJson(_GetListByFunc));
	    }, ec.current());
    }

    public CompletionStage<Result> CreatSubBill(){
		JsonNode json = request().body().asJson();
		String PhoneNumber = json.findPath("PhoneNumber").textValue();
		String DeliveryOrderid = json.findPath("DeliveryOrderid").textValue();
		String SubId = json.findPath("SubId").textValue();
		String DeliveryQty = json.findPath("DeliveryQty").textValue();
		String StartDate = json.findPath("StartDate").textValue();
		String EndDate = json.findPath("EndDate").textValue();
		String ExecPosi = json.findPath("ExecPosi").textValue();
		String ExecBy = json.findPath("ExecBy").textValue();
		
	    return deliveryInfo_Repository.CreatSubBill(PhoneNumber,DeliveryOrderid, SubId, DeliveryQty, StartDate, EndDate , ExecPosi , ExecBy).thenApplyAsync(_GetListByProc -> {
	    	return ok(Json.toJson(_GetListByProc));
	    }, ec.current());
    }
}
